#include "options_coords.h"

double CoordinatesOptions::reuseDefault_[7] = {7, 3, 1, 1, 1, 1, 0};
/** Nacte nastaveni z daneho XML elementu.
 * \param settings XML element s nastavenim obecne mapy
 */
void CoordinatesOptions::Read(ticpp::Element * settings) {
	bool error = false;
	// read positions and deands
	ticpp::Iterator<ticpp::Element> antena("antena");
	for (antena = antena.begin(settings); antena != antena.end(); ++antena) {
		double x = -1, y = -1;
		int demand;
		antena->GetAttribute("x", &x, false);
		antena->GetAttribute("y", &y, false);
		antena->GetAttributeOrDefault("demand", &demand, 1);
		if (x < 0 || y < 0 || demand < 0) {
			Log() << "Error: invalid antena settings" << std::endl;
			continue;
		}
		antenas.push_back(Antena(x, y, demand));
	}

	ticpp::Element * reuseElem = settings->FirstChildElement("reuse", false);
	if (!reuseElem) {
		Log() << "Error: No reuse distances set. Using standard (7, 3, 1, 1, 1, 0 - i.e. distance >= 7 - no constraint, >= 3 - minimum frequency distance = 1, >= 1 - minimum frequency distance = 2, same antena - minimum frequency distance = 5)." << std::endl;
		reuse = reuseDefault_;
		reuse_size = 7;
	}
	else {
		std::istringstream reuseValues(reuseElem->GetText(false));
		double d;
		std::list<double> reuseDistances;
		while (reuseValues >> d) {
			reuseDistances.push_back(d);
		}
		reuse_delete = true;
		reuse = new double[reuseDistances.size()];
		reuse_size = reuseDistances.size();
		int count = 0;
		for (std::list<double>::const_iterator i = reuseDistances.begin(); i != reuseDistances.end(); ++i) {
			reuse[count++] = *i;
		}		
	}
}

/** Vypise do logu nastaveni obecne mapy.
 */
void CoordinatesOptions::Print() {
	Log() << "Nastaveni vlastni mapy: " << std::endl << " - pozadavky: ";
	for (std::list<Antena>::const_iterator i = antenas.begin(); i != antenas.end(); ++i) {
		Log() << i->demand << "[" << i->x << ", " << i->y << "] ";
	}
	Log() << std::endl << " - znovupouzitelne vzdalenosti: ";
	for (int i = 0; i < reuse_size; ++i) {
		Log() << reuse[i] << ' ';
	}
	Log() << std::endl;
}
